/*
 *      debug.c
 *      
 *      Copyright 2009 Daniele Ricci <denn86@gmail.com>, Luca Saccagi <luksak86@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifndef _DEBUGF_IMPL
#define _DEBUGF_IMPL

#include "debug.h"


#ifdef _WIN32 
static void resProg(char *prefix,int Fcnt){
#else
void resProg(char *prefix,int Fcnt){
#endif
	int div=1;
	if(Fcnt==1){
		printf("%s",prefix);
	}
	fprintf(stdout,"%d",Fcnt);
	while(Fcnt/div>=1){
		div*=10;
		putchar('\b');
	}
	fflush(stdout);
}


#ifdef _WIN32
static void print_conf(conf *configuration){
#else
void print_conf(conf *configuration){
#endif
	printf("\nIDMP CONFIGURATION\n");
	printf("USER:_______%s_____\n",c0->usr);
	printf("PASSWORD:___%s_____\n",c0->pw);
	printf("CERT_LOC:___%s_____\n",c0->certificate);
	printf("CERT_KEY:___%s_____\n\n",c0->certificate_key);
	printf("DIR-STORE:__%s_____\n",c0->storedir);
	printf("DIR-TMP:____%s_____\n\n",c0->dynamicArchive);
	printf("IP:_________%s_____\n",c0->srv_ip);
	printf("PORT:_______%s_____\n\n",c0->srv_port);
	printf("REA_SIZE:___%d_____\n",c0->reallocation_size);
	printf("POL_TIME:___%d_____\n",c0->polling_time);
	printf("IDMEF_WIN:__%d_____\n",c0->idmef_window);
	printf("RDCT_RATE:__%d_____\n",c0->reduct_rate);
	printf("READ_RETR:__%d_____\n",c0->read_retries);
}


#ifdef _WIN32 
static void printTree(processingTree *root){	
#else
void printTree(processingTree *root){	
#endif
	if(root->sons!=NULL)printTree(root->sons);
	if(root->OP)
	printf("OP:%s,sign_num=%d,step_num=%d\n",root->OP,root->sign_num,root->step_num);
	if(root->nextbro!=NULL)printTree(root->nextbro);
}


#ifdef _WIN32 
static void printIndexStruct(int printtime, int printaddress){
#else
void printIndexStruct(int printtime, int printaddress){
#endif
	int i,cnt=0;
	iIp *src_cur;
	iIp *trg_cur;
	dailyAlertList *d_cur;
	yList *y_cur;

	if(printaddress == 1){	
		for(i=0;i<255;i++) {
			src_cur = indexIp[i].sourceItem;
			while(src_cur != NULL){
				src_cur=src_cur->down;
			}
			trg_cur = indexIp[i].targetItem;
			while(trg_cur != NULL){
				trg_cur=trg_cur->down;
			}
		}
		printf("cnt %d\n",cnt);
	}
		
	if(printtime == 1){
		for(i=0;i<366;i++){
			y_cur=CreateTime[i].top;
			while(y_cur != NULL){
				d_cur=y_cur->top;
				while(d_cur != NULL){
					d_cur=d_cur->next;
				}
				y_cur=y_cur->next;
			}
		}
		for(i=0;i<366;i++){
			y_cur=DetectTime[i].top;
			while(y_cur != NULL){
				d_cur=y_cur->top;
				while(d_cur != NULL){
					printf("DET %d-%d-%dT%d:%d:%dZ\n",d_cur->yy,d_cur->mm,d_cur->dd,d_cur->hh,d_cur->min,d_cur->sec);	
					d_cur=d_cur->next;
				}
				y_cur=y_cur->next;
			}
		}
		for(i=0;i<366;i++){
			y_cur=AnalyzerTime[i].top;
			while(y_cur != NULL){
				d_cur=y_cur->top;
				while(d_cur != NULL){
					printf("ANA %d-%d-%dT%d:%d:%dZ\n",d_cur->yy,d_cur->mm,d_cur->dd,d_cur->hh,d_cur->min,d_cur->sec);	
					d_cur=d_cur->next;
				}
				y_cur=y_cur->next;
			}
		}
	}
}


#ifdef _WIN32
static void print_xmlNodeList(xmlNodeList *list){
#else
void print_xmlNodeList(xmlNodeList *list){
#endif
	xmlNodeList *li=list;
	int printed=0,cnt=0;
	while(li!=NULL){
		cnt++;
		printf("%d(%p) [I->%p][N->%p]  \n",cnt,li,li->item,li->next);
		li=li->next;	
	}
	printf("\n");
}


#ifdef _WIN32
static int cnt_xmlNodeList(xmlNodeList *list){
#else
int cnt_xmlNodeList(xmlNodeList *list){
#endif
	xmlNodeList *li=list;
	int cnt=0;
	while(li!=NULL){
		cnt++;
		li=li->next;
	}
	return cnt;
}




#ifdef _WIN32 
static void printProp(xmlNode *this){
#else
void printProp(xmlNode *this){
#endif
	xmlAttr *prop;
	
	printf("DBG - %s  ",strtoupr((char *)this->name));
	prop=this->properties;
	while(prop!=NULL){
		printf("DBG - %s: %s  ",strtoupr((char *)prop->name),(char *)prop->CONTENT);
		prop=prop->next;
	}
	putchar('\n');	
}


#ifdef _WIN32 
static void printSons(xmlNode *this){
#else
void printSons(xmlNode *this){
#endif
	xmlNode *son;
	
	for(son = this->CHILDREN; son; son = son->NEXT){
		if(son->CONTENT!=NULL){
			printf("\t%s: %s\n",strtoupr((char *)son->name),son->CONTENT);
		}
	}	
}

#ifdef _WIN32 
static void printSubTree(xmlNode *root,int length, char *selstr,char *cmp){
#else
void printSubTree(xmlNode *root,int length, char *selstr,char *cmp){
#endif
	if(strncasecmp(selstr,cmp,length)==0){
		printProp(root);
		printSons(root);
	}
}
#endif